/*
 *  WObjectListener.h
 *  GLRender
 *
 *  Created by Patrick zulian on 5/21/10.
 *  Copyright 2010 University  of Lugano. All rights reserved.
 *
 */

#ifndef W_OBJECT_LISTENER_H
#define W_OBJECT_LISTENER_H
#if 0

#include <set>

class WObject;

class WObjectListener {
public:
	virtual
	void objectMoved(WObject * object, const int callbackNumber ) = 0;
	
	//virtual
//	void objectDied(WObject * object) { }
};

/** ONLY TO BE USED WITH THE COLLISION SYSTEM **/
class WListenable {
private:
	std::set< WObjectListener * > listeners;
public:
	inline
	void addListener(WObjectListener * listener)
	{
		listeners.insert(listener);
	}
	
	inline
	void removeListener(WObjectListener * listener)
	{
		
		for ( std::set< WObjectListener * >::iterator it = listeners.begin(); it != listeners.end(); ++it ) {
			if ((*it) == listener) {
				listeners.erase(it);
				return;
			}
			
		}
		
		assert(false);
		//const int size = (int) listeners.size();
//		listeners.erase(listener);
//		const int newSize = (int) listeners.size();
//		assert(size >  newSize);
	}
	
	inline
	void clear()
	{
		listeners.clear();
	}
	
	
	inline
	void fireMoved()
	{
		if (listeners.empty()) {
			return;
		}
		printf("number of listeners %d\n", (int) listeners.size());
		int callbackNumber = 0;
		for ( std::set< WObjectListener * >::iterator it = listeners.begin(); it != listeners.end(); ++it ) {
			WObject * that = getMe();
			(*it)->objectMoved(that, callbackNumber++);
			
		}
	}
	
	
protected:	
	virtual
	WObject * getMe() = 0;

	
};
#endif
#endif
